package com.dxysun.springboot.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 *  拦截器
 */
public class MyInterceptor implements HandlerInterceptor
{

	private static final Log logger = LogFactory.getLog(MyInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		//获取登录时保存到session的用户信息
		logger.info("MyInterceptor preHandle");
		logger.info("MyInterceptor handler:" + handler);
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception
	{
		logger.info("MyInterceptor postHandle");

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception
	{
		logger.info("MyInterceptor afterCompletion");
		logger.info("MyInterceptor afterCompletion handler:" + handler);
		logger.info("MyInterceptor afterCompletion handler:" + handler.getClass().getSimpleName());
		HandlerMethod handlerMethod = (HandlerMethod) handler;
//		handlerMethod.
//		response.getOutputStream().write("afterCompletion error".getBytes());
//		response.getOutputStream().close();
//		response.getWriter()
//		response.getWriter().write("afterCompletion error");
//		response.getWriter().close();
	}

}